﻿using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkflowFramework
{
    public abstract class WorkFlowBase
    {
        public abstract string Name { get; }
        public string Id = Guid.NewGuid().ToString();
        public IWorkFlowExecutor Executor;//工作流执行器
        public bool IsRunning { get; private set; }
        public abstract void Build(IWorkFlowBuilder builder);
        public async Task Start()
        {
            if (Executor != null && !IsRunning)
            {
                // 启动流程执行
                IsRunning = true;
                Log.Information("{0} Start", Name);
                await Executor.ExecuteFlowAsync();
                Log.Information("{0} End", Name);
                IsRunning = false;
            }
        }
        /// <summary>
        /// 暂停
        /// </summary>
        /// <returns></returns>
        public async Task Pause()
        {
            if (Executor != null && IsRunning && !Executor.IsPaused)
            {
                // 启动流程执行
                await Executor.ProcessUserCommandAsync(WorkFlowCommand.Pause);
            }
            //处理子流程
            await BranchHandler(Executor.Steps, WorkFlowCommand.Pause);
        }
        public async Task Continue()
        {
            if (Executor != null && IsRunning && Executor.IsPaused)
            {
                // 启动流程执行
                await Executor.ProcessUserCommandAsync(WorkFlowCommand.Continue);
            }
            //处理子流程
            await BranchHandler(Executor.Steps, WorkFlowCommand.Continue);
        }
        public async Task Retry()
        {
            if (Executor != null && IsRunning && Executor.IsPaused)
            {
                // 启动流程执行
                await Executor.ProcessUserCommandAsync(WorkFlowCommand.Retry);


            }
            //处理子流程
            await BranchHandler(Executor.Steps, WorkFlowCommand.Retry);
        }
        public async Task Terminate()
        {
            if (Executor != null && IsRunning && Executor.IsPaused)
            {
                // 启动流程执行
                await Executor.ProcessUserCommandAsync(WorkFlowCommand.Terminate);
            }
            //处理子流程
            await BranchHandler(Executor.Steps, WorkFlowCommand.Terminate);
        }

        private async Task BranchHandler(IList<IFlowStep> brachSteps, WorkFlowCommand command)
        {
            foreach (var step in brachSteps)
            {
                if (step is BranchFlowStep)
                {
                    var branchFlowStep = step as BranchFlowStep;
                    if (branchFlowStep == null)
                    {
                        return;
                    }
                    if (command.Equals(WorkFlowCommand.Pause))
                    {
                        if (branchFlowStep.TrueBranch != null && !branchFlowStep.TrueBranch.IsPaused)
                        {
                            await branchFlowStep.TrueBranch.ProcessUserCommandAsync(command);
                        }
                        if (branchFlowStep.FalseBranch != null && !branchFlowStep.FalseBranch.IsPaused)
                        {
                            await branchFlowStep.FalseBranch.ProcessUserCommandAsync(command);
                        }
                    }
                    else
                    {
                        if (branchFlowStep.TrueBranch != null && branchFlowStep.TrueBranch.IsPaused)
                        {
                            await branchFlowStep.TrueBranch.ProcessUserCommandAsync(command);
                        }
                        if (branchFlowStep.FalseBranch != null && branchFlowStep.FalseBranch.IsPaused)
                        {
                            await branchFlowStep.FalseBranch.ProcessUserCommandAsync(command);
                        }
                    }
                }
            }
        }
    }
}
